package com.edu.schoolforum.service;

import cn.hutool.db.PageResult;
import com.edu.schoolforum.dto.AvatarUrlRequestDto;
import com.edu.schoolforum.dto.MpageRequestDto;
import com.edu.schoolforum.dto.PageRequestDto;
import com.edu.schoolforum.dto.UpdateUserLikePostDto;
import com.edu.schoolforum.mapper.PostMapper;
import com.edu.schoolforum.pojo.*;
import com.edu.schoolforum.utils.CommonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tao
 * @date 2021/8/8 14:12
 */
@Service
public class PostServiceImpl implements PostService{
    @Autowired
    PostMapper postMapper;
    @Override
    public List<Post> selectPage(String plate) {
        return postMapper.selectPage(plate);
    }

    @Override
    public CommonResult findPage(PageRequestDto pageRequest) {

        return CommonResult.success(getPageInfo(pageRequest));
    }

    @Override
    public CommonResult mfindPage(MpageRequestDto pageRequest) {
        return CommonResult.success(mGetPageInfo(pageRequest));
    }

    @Override
    public List<Post> selectAllPost() {
        return postMapper.selectAllPost();
    }

    @Override
    public int insertPost(Post post) {
        return postMapper.insertPost(post);
    }

    @Override
    public int insertReplyPost(ReplyPost replyPost) {
        return postMapper.insertReplyPost(replyPost);
    }

    @Override
    public List<ReplyPost> selectReplyPost(Integer id) {
        return postMapper.selectReplyPost(id);
    }

    @Override
    public Post selectOnePost(Integer id) {
        return postMapper.selectOnePost(id);
    }

    @Override
    public int updatePostLikes(PostLikes postLikes) {
        return postMapper.updatePostLikes(postLikes);
    }

    @Override
    public int getReplyPostCount(Integer id) {
        return postMapper.getReplyPostCount(id);
    }

    @Override
    public int insertLikeMessage(LikeMessage lik) {
        return postMapper.insertLikeMessage(lik);
    }

    @Override
    public int getUserAllLikesCount(String name) {
        return postMapper.getUserAllLikesCount(name);
    }

    @Override
    public List<Post> getDimPost(String dimTitle) {
        return postMapper.getDimPost(dimTitle);
    }

    @Override
    public LikeMessage getLikeMessage(LMessage lMessage) {
        return postMapper.getLikeMessage(lMessage);
    }

    @Override
    public int getPostLikeStatus(LMessage lMessage) {
        return postMapper.getPostLikeStatus(lMessage);
    }

    @Override
    public int updatePostLikeStatus(UpdateUserLikePostDto updateUserLikePostDto) {
        return postMapper.updatePostLikeStatus(updateUserLikePostDto);
    }

    @Override
    public int getUserPostCount(String userName) {
        return postMapper.getUserPostCount(userName);
    }

    @Override
    public int getUserPostCommentCount(String userName) {
        return postMapper.getUserPostCommentCount(userName);
    }

    @Override
    public int CommentCountAdd(Integer id) {
        return postMapper.CommentCountAdd(id);
    }

    @Override
    public List<Post> getUserPosts(String userName) {
        return postMapper.getUserPosts(userName);
    }

    @Override
    public int deleteUserPost(LMessage lMessage) {
        return postMapper.deleteUserPost(lMessage);
    }

    @Override
    public int deletePostComment(Integer id) {
        return postMapper.deletePostComment(id);
    }

    @Override
    public int getPlatePostCount(String plate) {
        return postMapper.getPlatePostCount(plate);
    }

    @Override
    public int updatePostAvatar(AvatarUrlRequestDto avatarUrlRequestDto) {
        return postMapper.updatePostAvatar(avatarUrlRequestDto);
    }

    @Override
    public int updateReplyAvatar(AvatarUrlRequestDto avatarUrlRequestDto) {
        return postMapper.updateReplyAvatar(avatarUrlRequestDto);
    }

    /**
     * 调用分页插件完成分页(传参数有两个，一个页数一个板块名)
     * @param
     * @return
     */
    private PageInfo<Post> getPageInfo(PageRequestDto pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = 5;
        PageHelper.startPage(pageNum, pageSize);
        List<Post> sysMenus = postMapper.selectPage(pageRequest.getPlate());
        return new PageInfo<Post>(sysMenus);
    }
    /**
     * 调用分页插件完成分页(传参数有一个（首页）)
     * @param
     * @return
     */
    private PageInfo<Post> mGetPageInfo(MpageRequestDto pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = 5;
        PageHelper.startPage(pageNum, pageSize);
        List<Post> sysMenus = postMapper.mselectPage();
        return new PageInfo<Post>(sysMenus);
    }



}
